/*	kd:

	Each sprite can fade, flash and bounce. There's probably a case to be made
	for button classes here but I have a bad feeling about that.
	
	So here you can set stuff like button positions, animation direction, time,
	overlay flashes, bouncing, etc. in a manner I consider KIIIIIINDA simple.
	It creates a nice overview like this.
	
	The MOST IMPORTANT bit by far is that you can move the buttons by any
	sprite amount. In hindsight, it's a bit weird anyway, cuz you'd think you
	could just use whatever textureid was stored... oh well, better luck
	next time.

	*/

struct Gc_Shop_ButtonDrawing {
	Gc_UiSprites					sprites;
	Gc_UiAnim						move_anim;
	private Gc_UiSprites			overlays;
	private Gc_UiDirAnim			bounce_anim;
	private Gc_UiDirAnim			flash_anim;
	private Gc_UiDirAnim			hover_anim;
	private Gc_UiDirAnim			base_anim;
	
	private Gc_NormalFades			alpha_t;
	private Gc_NormalFades			flash_t;
	private Gc_NormalFades			bounce_t;
	private int						sprite_count;
	
	// kd: The shopkeeper button is special cuz it has a face on it.
	private TextureId				shopkeeper_sprite;
	private double					shopkeeper_offset;
	
	private Array <bool>			has_overlay;
	
	// kd: You only have to call this once.
	void OnBuild (Gc_Shop_UiHandling ui_) {
		let base_alpha = 0.4;
		
		// kd: I'd like to say that here, ui_ isn't REALLY needed... it's just
		// convenient, cuz you can be sure the calling code has an instance of
		// this. That means... you get to write ui_ instead of
		// Gc_Shop_UiHandling all the time, and put it into a parameter.
		
		sprite_count = ui_.total_sprite_count;
		
		has_overlay.clear();
		has_overlay.resize(sprite_count);
		
		base_pos_x.clear();
		base_pos_y.clear();
		base_pos_x.resize(sprite_count);
		base_pos_y.resize(sprite_count);
		
		overlays.	ClearAndResize(sprite_count);
		sprites.	ClearAndResize(sprite_count);
		move_anim.	ClearAndResize(sprite_count);
		base_anim.	ClearAndResize(sprite_count);
		bounce_anim.ClearAndResize(sprite_count);
		flash_anim.	ClearAndResize(sprite_count);
		hover_anim.	ClearAndResize(sprite_count);
		alpha_t.	ClearAndResize(sprite_count);
		flash_t.	ClearAndResize(sprite_count);
		bounce_t.	ClearAndResize(sprite_count);
		
		MakeFadeAnim(ui_.left,			"loll",		(-0.05,  0.5), (0.1, 0.5));
		MakeFadeAnim(ui_.right,			"lolr",		( 1.05,  0.5), (0.9, 0.5));
		MakeFadeAnim(ui_.scroll_up,		"datscrol",	( 0.5,   1.2), (0.5, 0.88666));
		MakeFadeAnim(ui_.scroll_down,	"datscrod",	( 0.5,   1.2), (0.5, 0.88666));
		MakeFadeAnim(ui_.message_box,	"widebar2",	( 0.5,   1.2), (0.5, 0.88666));
		MakeFadeAnim(ui_.switch_keeper,	"datbutn2",	( 0.9,  -0.2), (0.9, 0.04));
		MakeFadeAnim(ui_.fx,			"datbutn2",	( 0.9,  -0.2), (0.9, 0.04));
		MakeFadeAnim(ui_.fx_on,			"partyon",	( 0.9,  -0.2), (0.9, 0.04));
		MakeFadeAnim(ui_.fx_off,		"partyoff",	( 0.9,  -0.2), (0.9, 0.04));
		MakeFadeAnim(ui_.favorite,		"datbutn2",	( 0.1,  -0.2), (0.1, 0.04));
		MakeFadeAnim(ui_.favorite_on,	"starman2", ( 0.1,  -0.2), (0.1, 0.04));
		MakeFadeAnim(ui_.favorite_off,	"starman3", ( 0.1,  -0.2), (0.1, 0.04));
		// MakeFadeAnim(ui_.alpha_slider,	"datbutn",	( 0.9,  -0.1), (0.9, 0.05));
		
		MakeHoverAnim(ui_.left,				base_alpha, 0.9, 10, (-0.2, 0));
		MakeHoverAnim(ui_.right,			base_alpha, 0.9, 10, ( 0.2, 0));
		MakeHoverAnim(ui_.scroll_up,		0.2, 1.0, 5);
		MakeHoverAnim(ui_.scroll_down,		0.2, 1.0, 5);
		MakeHoverAnim(ui_.message_box,		base_alpha, 0.9, 10);
		MakeHoverAnim(ui_.switch_keeper,	base_alpha, 0.9, 10);
		MakeHoverAnim(ui_.favorite,			base_alpha, 0.9, 10);
		MakeHoverAnim(ui_.favorite_on,		0, 1.0, 13);
		MakeHoverAnim(ui_.favorite_off,		0.2, 0, 10);
		MakeHoverAnim(ui_.fx,				base_alpha, 0.9, 10);
		MakeHoverAnim(ui_.fx_on,			0, 0.8, 13);
		MakeHoverAnim(ui_.fx_off,			0.8, 0, 10);
		// MakeHoverAnim(ui_.alpha_slider,		base_alpha, 0.9, 10);
		
		MakeClickAnim(ui_.left,				"lollover", 1.0, 10, (-0.5, 0), 14);
		MakeClickAnim(ui_.right,			"lolrover", 1.0, 10, ( 0.5, 0), 14);
		MakeClickAnim(ui_.scroll_up,		"datscrol", 1.0, 15);
		MakeClickAnim(ui_.scroll_down,		"datscrod", 1.0, 15);
		MakeClickAnim(ui_.switch_keeper,	"datbotn2", 1.0, 15);
		MakeClickAnim(ui_.favorite,			"datbotn2", 1.0, 15);
		MakeClickAnim(ui_.favorite_on,		"starover",	1.0, 10);
		MakeClickAnim(ui_.fx,				"datbotn2", 1.0, 15);
		MakeClickAnim(ui_.fx_on,			"fxover",	1.0, 10);
	}
	
	void OnInit (vector2 resolution, Gc_Shop_UiHandling ui_) {
		move_anim.OnInit(resolution);
		
		// kd: Sometimes, you'll want your sprites attached to another. With the
		// normal coordinates, that's impossible. We need to know about the
		// sprite dimensions you've used. This does it in a kinda simple way.
		let sprite_left		= sprites.At(ui_.left);
		let sprite_right	= sprites.At(ui_.right);
		let	sprite_box		= sprites.At(ui_.message_box);
		let	sprite_up		= sprites.At(ui_.scroll_up);
		let	sprite_down		= sprites.At(ui_.scroll_down);
		let	sprite_switch	= sprites.At(ui_.switch_keeper);
		let	sprite_favorite	= sprites.At(ui_.favorite);
		let sprite_fx		= sprites.At(ui_.fx);
		
		hover_anim.ScaleBySprite(ui_.left,		sprite_left);
		hover_anim.ScaleBySprite(ui_.right,		sprite_right);
		bounce_anim.ScaleBySprite(ui_.left,		sprite_left);
		bounce_anim.ScaleBySprite(ui_.right,	sprite_right);
		
		move_anim.TranslateBySprite(ui_.switch_keeper,	sprite_switch,	(0.0, 0.5));
		move_anim.TranslateBySprite(ui_.fx,				sprite_fx,		(0.0, 1.5));
		move_anim.TranslateBySprite(ui_.fx_on,			sprite_fx,		(0.0, 1.5));
		move_anim.TranslateBySprite(ui_.fx_off,			sprite_fx,		(0.0, 1.5));
		move_anim.TranslateBySprite(ui_.favorite,		sprite_favorite,(0.0, 0.5));
		move_anim.TranslateBySprite(ui_.favorite_on,	sprite_favorite,(0.0, 0.5));
		move_anim.TranslateBySprite(ui_.favorite_off,	sprite_favorite,(0.0, 0.5));
		// move_anim.TranslateBySprite(ui_.alpha_slider,	sprite_switch,	(1.0, 0.5));
		move_anim.TranslateBySprite(ui_.scroll_down,	sprite_box,		(0.5, 0.5));
		move_anim.TranslateBySprite(ui_.scroll_up,		sprite_box,		(0.5, -0.5));
		move_anim.TranslateBySprite(ui_.scroll_down,	sprite_down,	(-0.5, -0.5));
		move_anim.TranslateBySprite(ui_.scroll_up,		sprite_up,		(-0.5, 0.5));
		move_anim.TranslateAbs(ui_.scroll_up, (-12, 16));
	}
	
	// kd: There are two buttons that need SPECIAL SPECIAL treatment...
	void OnUpDownInit (Gc_Shop_UiHandling ui_, int line_count = 1) {
		let sprite_left		= sprites.At(ui_.left);
		let sprite_right	= sprites.At(ui_.right);
		let	sprite_box		= sprites.At(ui_.message_box);
		let	sprite_up		= sprites.At(ui_.scroll_up);
		let	sprite_down		= sprites.At(ui_.scroll_down);
		let	sprite_switch	= sprites.At(ui_.switch_keeper);
		
		move_anim.Copy(ui_.scroll_up, ui_.scroll_down);
		move_anim.TranslateAbs(ui_.scroll_down, max(1, line_count - 1) * (0, 8));
	}
	
	void OnInitShopkeeper (Gcs_Shopkeeper shopkeeper) {
		shopkeeper_sprite = Gc_Sprite.Load(shopkeeper ? shopkeeper.icon : "tnt1a0");
		
		let size = TexMan.GetScaledSize(shopkeeper_sprite);
		shopkeeper_offset = 0.5 * size.y;
	}
	
	void NextState () {
		for(let i = 0; i < sprite_count; i++) {
			if(1.0 <= flash_t.At(i)) {
				flash_t.ForceToStart(i);
			}
			
			if(1.0 <= bounce_t.At(i)) {
				bounce_t.ForceToStart(i);
			}
		}
		
		bounce_t.NextState();
		flash_t.NextState();
		alpha_t.NextState();
	}
	
	void Hover (int button) {
		alpha_t.ToEnd(button, hover_anim.def_tics [button]);
	}
	
	void Dehover (int button) {
		alpha_t.ToStart(button, hover_anim.def_tics [button]);
	}
	
	void Click (int button) {
		flash_t.ForceToStart(button);
		bounce_t.ForceToStart(button);
		flash_t.ToEnd(button, flash_anim.def_tics [button]);
		bounce_t.ToEnd(button, bounce_anim.def_tics [button]);
	}
	
	// kd: This takes sprites or buttons.
	private void MakeFadeAnim (
	int				index,
	String			sprite_name,
	vector2			start_normal,
	vector2			end_normal) {
		sprites.Load(index, sprite_name);
		move_anim.Set(index, start_normal, end_normal);
	}
	
	// kd: This only takes buttons.
	private void MakeClickAnim (
	int			index,
	String		sprite_name,
	double		flash_alpha		= 0,
	int			flash_tics		= 0,
	vector2		bounce_offset 	= (0, 0),
	int			bounce_tics		= 0) {
		overlays.Load	(index, sprite_name);
		flash_anim.Set	(index, flash_tics, flash_alpha);
		bounce_anim.Set	(index, bounce_tics, direction: bounce_offset);
		has_overlay [index] = true;
	}
	
	// kd: This takes buttons and sprites. Usually, only buttons' hover anims
	// are triggered, but as you can see, this also defines the alpha the sprite
	// will be drawn at by default.
	private void MakeHoverAnim (
	int			index,
	double		min_alpha		= 1,
	double		max_alpha		= 1,
	int			tics			= 10,
	vector2		offset			= (0, 0)) {
		base_anim.Set(index, alpha: min_alpha);
		hover_anim.Set(index, tics, max_alpha, offset);
	}
	
	private Array <double>			base_pos_x;
	private Array <double>			base_pos_y;
	
	ui vector2 DrawPos (int index) const {
		return (
			base_pos_x [index],
			base_pos_y [index]);
	}
	
	// kd: This caches some draw data that we don't need to do every frame,
	// just as long as the transition animations happen (intro and exit).
	ui void NextFadeData (double anim_f = 1) const /* whistles */ {
		for(let i = 0; i < sprite_count; i++) {
			base_pos_x [i] = move_anim.pos_x [i] + anim_f * move_anim.diff_x [i];
			base_pos_y [i] = move_anim.pos_y [i] + anim_f * move_anim.diff_y [i];
		}
	}
	
	ui void DrawShopkeeperFace (vector2 resolution, double alpha = 0.777) const {
		Screen.DrawTexture(
			shopkeeper_sprite,
			true,		// kd: allows animdefs to work
			base_pos_x [Gc_Shop_UiHandling.switch_keeper],
			base_pos_y [Gc_Shop_UiHandling.switch_keeper],
			DTA_LEGACYRENDERSTYLE,	STYLE_TRANSLUCENT,
			DTA_KEEPRATIO,			true,
			DTA_CENTEROFFSET,		true,
			DTA_VIRTUALWIDTHF,		resolution.x,
			DTA_VIRTUALHEIGHTF,		resolution.y,
			DTA_ALPHA,				alpha);
	}
	
	ui void Draw (
	int		start,
	int		end,
	vector2 resolution,
	double	t		= 1,
	int		style	= STYLE_TRANSLUCENT) const {
		for(let i = start; i < end; i++) {
			let alpha_f	= Gc_Interpol.trerpfast_unit(alpha_t.At(i, t));
			
			let min_alpha	= base_anim.def_alpha [i];
			let diff_alpha	= hover_anim.def_alpha [i] - min_alpha;
			let alpha		= min_alpha + alpha_f * diff_alpha;
			
			let hover_offs	= alpha_f * (
				hover_anim.off_x [i],
				hover_anim.off_y [i]);
			
			let bounce_f = bounce_t.At(i, t);
			bounce_f = Gc_Interpol.trerpfast_unit(
				bounce_f < 0.5 ?
				2 * bounce_f :
				2.0 - 2 * bounce_f);
			
			let bounce_offs	= bounce_f * (
				bounce_anim.off_x [i],
				bounce_anim.off_y [i]);
			
			let draw_pos	=
				(base_pos_x [i], base_pos_y [i]) + hover_offs + bounce_offs;
			
			Screen.DrawTexture(
				sprites.At(i),
				true,		// kd: allows animdefs to work
				draw_pos.x,
				draw_pos.y,
				DTA_LEGACYRENDERSTYLE,	style,
				DTA_KEEPRATIO,			true,
				DTA_CENTEROFFSET,		true,
				DTA_VIRTUALWIDTHF,		resolution.x,
				DTA_VIRTUALHEIGHTF,		resolution.y,
				DTA_ALPHA,				alpha);
			
			if(has_overlay [i]) {
				let flash_f = 1.0 - flash_t.at(i, t);
				
				if(flash_f < 1) {
					alpha = flash_f * flash_anim.def_alpha [i];
					
					Screen.DrawTexture(
						overlays.At(i),
						true,
						draw_pos.x,
						draw_pos.y,
						DTA_LEGACYRENDERSTYLE,	STYLE_ADD,
						DTA_KEEPRATIO,			true,
						DTA_CENTEROFFSET,		true,
						DTA_VIRTUALWIDTHF,		resolution.x,
						DTA_VIRTUALHEIGHTF,		resolution.y,
						DTA_ALPHA,				alpha);
				}
			}
		}
	}
}

